package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.reflect.TypeToken
import com.jt.assistant.R
import com.jt.assistant.adapter.DocumentAdapter
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.base.GlobalConfig.DELIVERYRECEIPTID
import com.jt.assistant.bean.Param
import com.jt.assistant.bean.ScanCarton
import com.jt.assistant.bean.Waybills
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_document_details.*
import kotlinx.android.synthetic.main.activity_document_details.bt_commit
import kotlinx.android.synthetic.main.activity_order_selection.*

/**
 * 单据明细
 */
class DocumentDetailsActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        DocumentAdapter(
            ArrayList<Waybills>()
        )
    }

    override fun layoutId(): Int = R.layout.activity_document_details

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_Document) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
        mAdapter.data.addAll(
            GsonUtils.fromJson<java.util.ArrayList<Waybills>>(
                intent.getStringExtra("waybillList"),
                object : TypeToken<java.util.ArrayList<Waybills>>() {}.type
            )
        )
        mAdapter.notifyDataSetChanged()
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            inputDialog(position)
        }

        bt_commit.setOnClickListener {
            val params = Param::class.java.newInstance()
            params.type = intent.getStringExtra(GlobalConfig.ATYPE).toString()
            params.relation_id = intent.getStringExtra(GlobalConfig.RELATION_ID).toString()
            params.license_name = intent.getStringExtra(GlobalConfig.LICENSE_NAME).toString()
            params.waybillLists = mAdapter.data
            viewModel.deliveryReceiptCreate(params)
        }

        viewModel.mDeliveryReceiptCreate.observe(this, {
            if (it.isSuccess()) {
                startActivity(
                    Intent(
                        this@DocumentDetailsActivity,
                        CarReceiveUploadActivity::class.java
                    ).apply {
                        putExtra(DELIVERYRECEIPTID, it.data.deliveryReceiptId)
                    })
                finish()
            }
        })
    }


    private fun inputDialog(position: Int) {
        val dialog = MaterialDialog(this).show {
            title(text = mAdapter.data[position].channelOrderNo)
            customView(
                R.layout.layout_order_input,
                scrollable = false,
                horizontalPadding = false
            )
            lifecycleOwner(this@DocumentDetailsActivity)
        }
        val customView = dialog.getCustomView()
        val etV: EditText = customView.findViewById(R.id.et_v)
        val etW: EditText = customView.findViewById(R.id.et_w)
        val cancel: TextView = customView.findViewById(R.id.tv_cancle)
        val commit: TextView = customView.findViewById(R.id.tv_commit)

        etV.setText(mAdapter.data[position].volume.toString())
        etV.setSelection(mAdapter.data[position].volume.toString().length)

        etW.setText(mAdapter.data[position].weight.toString())
        etW.setSelection(mAdapter.data[position].weight.toString().length)

        cancel.setOnClickListener { dialog.dismiss() }
        commit.setOnClickListener {
            if (etV.text.isNotEmpty()) {
                mAdapter.data[position].volume = etV.text.toString().toDouble()
            }
            if (etW.text.isNotEmpty()) {
                mAdapter.data[position].weight = etW.text.toString().toDouble()
            }
            mAdapter.notifyDataSetChanged()
            dialog.dismiss()
        }
    }
}

